package com.yinshu.teleshield.manager.impl;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yinshu.teleshield.domain.entity.UserEntity;
import com.yinshu.teleshield.domain.vo.user.LoginResult;
import com.yinshu.teleshield.jwt.ContextUserDto;
import com.yinshu.teleshield.manager.UserManager;
import com.yinshu.teleshield.mapper.UserMapper;
import com.yinshu.teleshield.tkbase.manager.impl.SuperManagerImpl;
import com.yinshu.teleshield.utils.ColumnUtil;
import com.yinshu.teleshield.utils.CommonBeanUtils;
import com.yinshu.teleshield.utils.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * 用户表(User)表服务实现类
 *
 * @author yangjunxiong   2024-11-11 11:22:22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserManagerImpl extends SuperManagerImpl<UserMapper, UserEntity> implements UserManager {

    @Override
    public LoginResult byPhone(String phone) {
        UserEntity entity = this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getPhone, phone)
                .last(CommonConstant.LIMIT1)
        );
        if (Objects.isNull(entity)) {
            entity = new UserEntity();
            UserEntity newUser = saveNewUser(phone);
            CommonBeanUtils.copyProperties(newUser, entity);
        }
        LoginResult loginUserVO = new LoginResult();
        StpUtil.login(entity.getId(), SaLoginConfig
                .setExtra(ColumnUtil.getFieldName(ContextUserDto::getId), entity.getId().toString())
                .setExtra(ColumnUtil.getFieldName(ContextUserDto::getUsername), entity.getUsername())
                .setExtra(ColumnUtil.getFieldName(ContextUserDto::getNickname), entity.getNickname())
                .setExtra(ColumnUtil.getFieldName(ContextUserDto::getPhone), entity.getPhone())
                .setExtra(ColumnUtil.getFieldName(ContextUserDto::getTenantId), entity.getTenantId())
        );
        loginUserVO.setToken(StpUtil.getTokenValue());
        ContextUserDto contextUserDto = this.userInfo(entity);
        loginUserVO.setContextUserDto(contextUserDto);
        return loginUserVO;
    }

    private UserEntity saveNewUser(String phone) {
        UserEntity entity = new UserEntity();
        entity.setNickname(phone);
        entity.setUsername(phone);
        entity.setPhone(phone);
        entity.setStatus("1");
        entity.setSaveDef();
        entity.setTenantId(1L);
        this.save(entity);
        return entity;
    }

    @Override
    public LoginResult byToken(String phone) {
        UserEntity entity = this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getPhone, phone)
                .last(CommonConstant.LIMIT1)
        );
        LoginResult loginUserVO = new LoginResult();
        loginUserVO.setToken(StpUtil.getTokenValue());
        ContextUserDto contextUserDto = this.userInfo(entity);
        loginUserVO.setContextUserDto(contextUserDto);
        return loginUserVO;
    }

    private ContextUserDto userInfo(UserEntity entity) {
        ContextUserDto contextUserDto = new ContextUserDto();
        contextUserDto.setId(entity.getId());
        contextUserDto.setUsername(entity.getUsername());
        contextUserDto.setNickname(entity.getNickname());
        contextUserDto.setPhone(entity.getPhone());
        contextUserDto.setTenantId(entity.getTenantId());
        return contextUserDto;
    }
}
